package com.szcinda.rpa.service;

import com.szcinda.rpa.repository.User;
import com.szcinda.rpa.repository.UserRepository;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    private final UserRepository userRepository;
    private final PasswordEncoder passwordEncoder;

    public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder) {
        this.userRepository = userRepository;
        this.passwordEncoder = passwordEncoder;
    }


    @Override
    public List<User> findAll() {
        return userRepository.findAll();
    }

    @Override
    public void login(LoginDto loginDto) {
        User user = userRepository.findByAccount(loginDto.getAccount());
        Assert.isTrue(user != null, "用户名不存在");
        Assert.isTrue(loginDto.getPassword().equals(user.getPassword()), "密码不正确");
    }
}
